import requests
from bs4 import BeautifulSoup
import json

def process_changelog(page_section):
    changelog_entries = []
    current_entry = None
    
    for item in page_section.find_all(['p', 'li']):
        text = item.get_text(strip=True)
        
        # Check if the item is a version line
        if text.startswith('0.'):
            # If there's an existing entry, save it
            if current_entry:
                changelog_entries.append(current_entry)
            
            # Start a new entry
            current_entry = {
                'version': text,
                'date': None,  # Placeholder for date
                'title': None,  # Placeholder for title
                'content': []
            }
        elif ' - ' in text:
            # Assume this is a date and title line
            parts = text.split(' - ', 1)
            current_entry['date'] = parts[0]
            current_entry['title'] = parts[1]
        else:
            # Ensure current_entry is initialized before adding content
            if current_entry:
                current_entry['content'].append(text)
    
    # Add the last entry if it exists
    if current_entry:
        changelog_entries.append(current_entry)
    
    # Generate JSON file
    save_to_json(changelog_entries)


def save_to_json(data, filename='changelog.json'):
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=4, ensure_ascii=False)


# URL of the changelog page
url = 'https://www.cursor.com/cn/changelog'

# Send a GET request to the URL
response = requests.get(url)

# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')

# Find the page section
page_section = soup.find('div', class_='page')

# Check if the page section is found
if page_section:
    process_changelog(page_section)
    print('Changelog has been written to changelog.json')
else:
    print('Page section not found on the page.') 